﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AlgorithmResult;
//using SegGen;

namespace SimilarityToAncientCodon
{
    public class FragmentSimToAncientCodon
    {

        public FragmentSimToAncientCodon(Result result, string gene, int numNucleotidesIncodon)
        {
            int segmentStartIndex = 0;
            int segSize = 0;
            string _ancientCodon = null;
            string geneFragment = null;
            int nuclSimCodonCounter = 0;
            int fragmentStartInd = 0;
            int totalSimCodonCounter = 0;

            result.SimilarityToAncientCodon = new float[result.NumOfBoundaries];

            for (int i = 0; i < result.NumOfBoundaries; i++)
            {
                 nuclSimCodonCounter = 0;
                _ancientCodon = result.ArchreTriplets[i];

                if (segmentStartIndex == 0)
                    segSize = (int)result.BoundariesSet[segmentStartIndex];

                else if (segmentStartIndex < result.NumOfBoundaries)
                {
                    segSize = (int)(result.BoundariesSet[segmentStartIndex] - result.BoundariesSet[segmentStartIndex - 1]);
                }


                geneFragment = gene.Substring(fragmentStartInd, segSize);

                for (int nuclInd = 0; nuclInd < segSize; nuclInd += numNucleotidesIncodon)
                {

                    for (int tripletInd = 0; tripletInd < numNucleotidesIncodon; tripletInd++)
                    {
                        // equal nucleotides
                        if ((geneFragment[nuclInd + tripletInd].ToString().ToUpper()).CompareTo(_ancientCodon[tripletInd].ToString().ToUpper()) == 0)
                        {
                            nuclSimCodonCounter++;
                        }
                    }
                }// for similarity of fragment 

               result.SimilarityToAncientCodon[i]  = (float) nuclSimCodonCounter / segSize;
               fragmentStartInd = (int)(result.BoundariesSet[segmentStartIndex]);
               segmentStartIndex++;
               totalSimCodonCounter += nuclSimCodonCounter;
            } // end for numOfBoundaries

            // calculate the total similarity of result (individual) to ancient codons

            result.TotalIndividualSimilarity = (float)totalSimCodonCounter / (int)(result.BoundariesSet[segmentStartIndex - 1]);
        }
    }
}
